

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.shell.expect &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../search/" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../">Module code</a> &raquo;</li>
        
      <li>cup.shell.expect</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.shell.expect</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    **Guannan just made a wraper out of pexpect.**</span>
<span class="sd">    The original copyright belongs to the author of pexpect module.</span>
<span class="sd">    See it at http://pexpect.sourceforge.net/pexpect.html</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup.thirdp</span> <span class="k">import</span> <span class="n">pexpect</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;go&#39;</span><span class="p">,</span> <span class="s1">&#39;go_ex&#39;</span><span class="p">,</span> <span class="s1">&#39;checkssh&#39;</span><span class="p">,</span> <span class="s1">&#39;go_with_scp&#39;</span><span class="p">,</span> <span class="s1">&#39;lscp&#39;</span><span class="p">,</span> <span class="s1">&#39;dscp&#39;</span>
<span class="p">]</span>


<span class="k">def</span> <span class="nf">_do_expect_ex</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;ret 0 success 1 timeout others -1&quot;&quot;&quot;</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">pobj</span> <span class="o">=</span> <span class="n">pexpect</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="s1">&#39;/bin/bash&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;-c&#39;</span><span class="p">,</span> <span class="n">command</span><span class="p">],</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">b_print_stdout</span><span class="p">:</span>
            <span class="n">pobj</span><span class="o">.</span><span class="n">logfile</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">pobj</span><span class="o">.</span><span class="n">expect</span><span class="p">(</span>
            <span class="p">[</span><span class="s1">&#39;password:&#39;</span><span class="p">,</span> <span class="s1">&#39;continue connecting (yes/no)?&#39;</span><span class="p">],</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">pobj</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">passwd</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">pobj</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="s2">&quot;yes&quot;</span><span class="p">)</span>
            <span class="n">pobj</span><span class="o">.</span><span class="n">expect</span><span class="p">([</span><span class="s1">&#39;password:&#39;</span><span class="p">])</span>
            <span class="n">pobj</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">passwd</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">pobj</span><span class="o">.</span><span class="n">expect</span><span class="p">(</span><span class="n">pexpect</span><span class="o">.</span><span class="n">EOF</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">pexpect</span><span class="o">.</span><span class="n">TIMEOUT</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Connection timeout</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">except</span> <span class="n">pexpect</span><span class="o">.</span><span class="n">EOF</span><span class="p">:</span>
        <span class="n">pobj</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">pobj</span><span class="o">.</span><span class="n">exitstatus</span>
    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Connection close, error:</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">error</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;exitstatus&#39;</span><span class="p">:</span> <span class="n">ret</span><span class="p">,</span>
        <span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">:</span> <span class="n">pobj</span><span class="o">.</span><span class="n">exitstatus</span><span class="p">,</span>
        <span class="s1">&#39;result&#39;</span><span class="p">:</span> <span class="n">pobj</span><span class="o">.</span><span class="n">before</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">ret</span>


<span class="k">def</span> <span class="nf">_do_expect</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; invoke _do_expect_ex&quot;&quot;&quot;</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">_do_expect_ex</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">],</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">])</span>


<div class="viewcode-block" id="checkssh"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.checkssh">[docs]</a><span class="k">def</span> <span class="nf">checkssh</span><span class="p">(</span><span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    check if we can ssh to hostname. Return True if succeed, False otherwise.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_</span><span class="p">,</span> <span class="n">rev</span> <span class="o">=</span> <span class="n">go</span><span class="p">(</span>
        <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="s1">&#39;echo &quot;testSSH&quot;&#39;</span><span class="p">,</span>
        <span class="n">timeout</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">False</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">rev</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;testSSH&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="go"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.go">[docs]</a><span class="k">def</span> <span class="nf">go</span><span class="p">(</span>
    <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    deprecated, recommand using go_ex or go_with_scp</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;ssh </span><span class="si">%s</span><span class="s2">@</span><span class="si">%s</span><span class="s2"> &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;&quot;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">command</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_do_expect</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">_judge_ret</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;exitstatus&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">]):</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">msg</span> <span class="o">+</span> <span class="s1">&#39; </span><span class="se">\n</span><span class="s1"> &#39;</span> <span class="o">+</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]</span>
    <span class="k">return</span> <span class="kc">False</span>


<div class="viewcode-block" id="go_with_scp"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.go_with_scp">[docs]</a><span class="k">def</span> <span class="nf">go_with_scp</span><span class="p">(</span>
    <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
    <span class="n">host_tmp</span><span class="o">=</span><span class="s1">&#39;/tmp/&#39;</span><span class="p">,</span> <span class="n">remote_tmp</span><span class="o">=</span><span class="s1">&#39;/tmp/&#39;</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Recommand using this function to remotely execute cmds.</span>

<span class="sd">    go_witch_scp will write a temp script file and scp to hostname:[host_tmp].</span>
<span class="sd">    Then execute it and get the result back.</span>

<span class="sd">    :param host_tmp:</span>
<span class="sd">        temp folder for keeping the temporary script file (contains the cmd)</span>
<span class="sd">    :param remote_tmp:</span>
<span class="sd">        remote temp folder for keeping the temporary script file</span>
<span class="sd">    :param timeout:</span>
<span class="sd">        timeout</span>

<span class="sd">    :return:</span>
<span class="sd">        a dict with keys (&#39;exitstatus&#39; &#39;remote_exitstatus&#39; &#39;result&#39;)</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;exitstatus&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
        <span class="s1">&#39;remote_exitstatus&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
        <span class="s1">&#39;result&#39;</span><span class="p">:</span> <span class="s1">&#39;write host file fail&#39;</span>
    <span class="p">}</span>
    <span class="n">tmp_filename</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">CGeneratorMan</span><span class="p">()</span><span class="o">.</span><span class="n">get_uniqname</span><span class="p">()</span>
    <span class="n">host_file</span> <span class="o">=</span> <span class="n">host_tmp</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">tmp_filename</span>
    <span class="n">remote_file</span> <span class="o">=</span> <span class="n">remote_tmp</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">tmp_filename</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">host_file</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">host_file</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ret</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">lscp</span><span class="p">(</span><span class="n">host_file</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">remote_file</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">_judge_ret</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="s1">&#39;scp ret:&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ret</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39; sh </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="n">remote_file</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">go_ex</span><span class="p">(</span><span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39; rm -f </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="n">host_file</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">res</span><span class="p">:</span>
        <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;rm -f host_file fail, ret:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">res</span>
        <span class="k">return</span> <span class="n">ret</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39; rm -f </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="n">remote_file</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">go_ex</span><span class="p">(</span><span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">_judge_ret</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="s1">&#39;rm -f remote_file ret:&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">res</span>
    <span class="k">return</span> <span class="n">ret</span></div>


<div class="viewcode-block" id="go_ex"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.go_ex">[docs]</a><span class="k">def</span> <span class="nf">go_ex</span><span class="p">(</span>
    <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">command</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run [command] on remote [hostname] and return result. If you have a lot</span>
<span class="sd">    of escape sign in the command, recommand using go_with_scp</span>

<span class="sd">    :param timeout:</span>
<span class="sd">        execution timeout, by default 800 seconds</span>

<span class="sd">    :return:</span>
<span class="sd">        return a dict with keys (&#39;exitstatus&#39; &#39;remote_exitstatus&#39; &#39;result&#39;)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;ssh </span><span class="si">%s</span><span class="s2">@</span><span class="si">%s</span><span class="s2"> &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;&quot;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">command</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">_do_expect_ex</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ret</span></div>


<div class="viewcode-block" id="lscp"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.lscp">[docs]</a><span class="k">def</span> <span class="nf">lscp</span><span class="p">(</span>
    <span class="n">src</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">dst</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    copy [localhost]:src to [hostname]:[dst]</span>

<span class="sd">    :return:</span>
<span class="sd">        return a dict with keys (&#39;exitstatus&#39; &#39;remote_exitstatus&#39; &#39;result&#39;)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;scp -r </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">@</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">dst</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_do_expect_ex</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">lscp_prod</span><span class="p">(</span><span class="n">scpstr</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">dst_path</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    deprecated. Kept here for compatibility only.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;scp -r &#39;</span> <span class="o">+</span> <span class="n">scpstr</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">dst_path</span>
    <span class="k">return</span> <span class="n">_do_expect</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span>


<div class="viewcode-block" id="dscp"><a class="viewcode-back" href="../../../../cup.shell/#cup.shell.expect.dscp">[docs]</a><span class="k">def</span> <span class="nf">dscp</span><span class="p">(</span>
    <span class="n">hostname</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">src</span><span class="p">,</span> <span class="n">dst</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">9000</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="o">=</span><span class="kc">False</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    copy [hostname]:[src] to [localhost]:[dst].</span>

<span class="sd">    :return:</span>
<span class="sd">        return a dict with keys (&#39;exitstatus&#39; &#39;remote_exitstatus&#39; &#39;result&#39;)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;scp -r </span><span class="si">%s</span><span class="s1">@</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">hostname</span><span class="p">,</span> <span class="n">src</span><span class="p">,</span> <span class="n">dst</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_do_expect_ex</span><span class="p">(</span><span class="n">passwd</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">b_print_stdout</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>